package edu.uc.service;

import java.util.HashMap;

import edu.uc.dao.IUserDAO;
import edu.uc.dao.UserProviderStub;
import edu.uc.dto.User;

/**
 * UserServiceStub is a hard-coded implementation stub of IUserService.
 * @author Bander
 *
 */
public class UserServiceStub implements IUserService {
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean logon(String username, String password) throws Exception {
		//create UserProvider implementation to pull user objects from persistence
		IUserDAO userProvider = new UserProviderStub();
		
		//fetch all users into a hashmap
		HashMap<String, User> allUsers = userProvider.fetchAll();
		
		//get the user with specified username from the hashmap
		User user = allUsers.get(username);
		
		//if the user isn't null and the password is the specified password, accept the logon (return true)
		if(user != null && user.getPassword().equals(password))
		{
			return true;
		}
		
		return false;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void save(User user) throws Exception {
		//create UserProvider implementation to pull user objects from persistence
		IUserDAO userProvider = new UserProviderStub();
		
		//call the save method from UserProvider class
		userProvider.save(user);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public User getUser(String username) throws Exception {
		//create UserProvider implementation to pull user objects from persistence
		IUserDAO userProvider = new UserProviderStub();
		
		//fetch the user from the IUserDAO object
		User user = userProvider.fetch(username);
		
		return user;
	}

}
